package com.zjuh.goodboy.util;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PatternUtil {
	
	private static Map<String, Pattern> patterns = new HashMap<String, Pattern>();
	
	public static boolean matches(String regex, String content) {
		Pattern pattern = patterns.get(regex);
		if (pattern == null) {
			pattern = compilePattern(regex);
		}
		Matcher m = pattern.matcher(content);
		return m.matches();
	}
	
	private static Pattern compilePattern(String regex) {
		synchronized (patterns) {
			Pattern pattern = patterns.get(regex);
			if (pattern != null) {
				return pattern;
			}
			pattern = Pattern.compile(regex);
			patterns.put(regex, pattern);
			return pattern;
		}
	}
	
}
